package com.ttmall.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ttmall.common.pojo.EasyUIDataGridResult;
import com.ttmall.common.pojo.TaotaoResult;
import com.ttmall.common.utils.IDUtils;
import com.ttmall.mapper.TbItemDescMapper;
import com.ttmall.mapper.TbItemMapper;
import com.ttmall.mapper.TbItemParamItemMapper;
import com.ttmall.pojo.*;
import com.ttmall.service.ItemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * Created by tianyi on 2017/12/30.
 */
@Service
public class ItemServiceImpl implements ItemService {

    @Autowired
    private TbItemMapper itemMapper;
    @Autowired
    private TbItemDescMapper tbItemDescMapper;
    @Autowired
    private TbItemParamItemMapper tbItemParamItemMapper;

    @Override
    public TaotaoResult getItemById(long itemId) {
        TbItemExample example = new TbItemExample();
        TbItemExample.Criteria criteria = example.createCriteria();
        criteria.andIdEqualTo(itemId);
        List<TbItem> list = itemMapper.selectByExample(example);
        if (list != null && list.size() > 0) {
            return TaotaoResult.ok(list.get(0));
        }
        return TaotaoResult.ok();
    }

    @Override
    public TaotaoResult getItemDescById(long itemId) {
        TbItemDescExample example = new TbItemDescExample();
        TbItemDescExample.Criteria criteria = example.createCriteria();
        criteria.andItemIdEqualTo(itemId);
        List<TbItemDesc> list = tbItemDescMapper.selectByExampleWithBLOBs(example);
        if (list != null && list.size() > 0) {
            return TaotaoResult.ok(list.get(0));
        }
        return TaotaoResult.ok();
    }

    @Override
    public EasyUIDataGridResult getItemList(int page, int rows) {
        TbItemExample example = new TbItemExample();
        PageHelper.startPage(page, rows);
        List<TbItem> tbItems = itemMapper.selectByExample(example);
        PageInfo<TbItem> tbItemPageInfo = new PageInfo<TbItem>(tbItems);
        EasyUIDataGridResult result = new EasyUIDataGridResult();
        result.setTotal(tbItemPageInfo.getTotal());
        result.setRows(tbItems);
        return result;
    }

    @Override
    public TaotaoResult createItem(TbItem tbItem, String desc, String itemParam) {
        Date now = new Date();
        long itemId = IDUtils.genItemId();

        tbItem.setId(itemId);
        //商品状态，1-正常，2-下架，3-删除
        tbItem.setStatus((byte)1);
        tbItem.setCreated(now);
        tbItem.setUpdated(now);
        itemMapper.insert(tbItem);

        TbItemDesc tbItemDesc = new TbItemDesc();
        tbItemDesc.setItemId(itemId);
        tbItemDesc.setItemDesc(desc);
        tbItemDesc.setCreated(now);
        tbItemDesc.setUpdated(now);
        tbItemDescMapper.insert(tbItemDesc);

        //添加商品规格参数
        TbItemParamItem tbItemParamItem = new TbItemParamItem();
        tbItemParamItem.setItemId(itemId);
        tbItemParamItem.setParamData(itemParam);
        tbItemParamItem.setCreated(now);
        tbItemParamItem.setUpdated(now);
        tbItemParamItemMapper.insert(tbItemParamItem);

        return TaotaoResult.ok();
    }

    @Override
    public TaotaoResult deleteItems(List<Long> ids) {
        TbItemExample example = new TbItemExample();
        TbItemExample.Criteria criteria = example.createCriteria();
        criteria.andIdIn(ids);
        itemMapper.deleteByExample(example);

        //删除描述
        TbItemDescExample example2 = new TbItemDescExample();
        TbItemDescExample.Criteria criteria2 = example2.createCriteria();
        criteria2.andItemIdIn(ids);
        tbItemDescMapper.deleteByExample(example2);

        //删除规格
        TbItemParamItemExample example3 = new TbItemParamItemExample();
        TbItemParamItemExample.Criteria criteria3 = example3.createCriteria();
        criteria3.andItemIdIn(ids);
        tbItemParamItemMapper.deleteByExample(example3);

        return TaotaoResult.ok();
    }

    @Override
    public TaotaoResult updateItem(TbItem tbItem, String desc, Long itemParamId, String itemParams) {
        Date now = new Date();

        tbItem.setUpdated(now);
        itemMapper.updateByPrimaryKeySelective(tbItem);

        TbItemDesc tbItemDesc = new TbItemDesc();
        tbItemDesc.setItemId(tbItem.getId());
        tbItemDesc.setItemDesc(desc);
        tbItemDesc.setUpdated(now);
        tbItemDescMapper.updateByPrimaryKeySelective(tbItemDesc);

        //添加商品规格参数
        TbItemParamItem tbItemParamItem = new TbItemParamItem();
        tbItemParamItem.setId(itemParamId);
        tbItemParamItem.setParamData(itemParams);
        tbItemParamItem.setUpdated(now);
        tbItemParamItemMapper.updateByPrimaryKeySelective(tbItemParamItem);

        return TaotaoResult.ok();
    }

    @Override
    public TaotaoResult instockItems(List<Long> idLongs) {
        Date now = new Date();
        TbItemExample example = new TbItemExample();
        TbItemExample.Criteria criteria = example.createCriteria();
        criteria.andIdIn(idLongs);

        TbItem tbItem = new TbItem();
        //商品状态，1-正常，2-下架，3-删除
        tbItem.setStatus((byte)2);
        tbItem.setUpdated(now);
        itemMapper.updateByExampleSelective(tbItem, example);
        return TaotaoResult.ok();
    }

    @Override
    public TaotaoResult reshelfItems(List<Long> idLongs) {
        Date now = new Date();
        TbItemExample example = new TbItemExample();
        TbItemExample.Criteria criteria = example.createCriteria();
        criteria.andIdIn(idLongs);

        TbItem tbItem = new TbItem();
        //商品状态，1-正常，2-下架，3-删除
        tbItem.setStatus((byte)1);
        tbItem.setUpdated(now);
        itemMapper.updateByExampleSelective(tbItem, example);
        return TaotaoResult.ok();
    }

}
